package com.es.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 获取数据库连接的类
 */
public class TestDBConnection {
    public static void main(String[] args) {
        Connection mysqlConnection = getMysqlConnection();
        //1.加载驱动
        // Class.forName()
        Connection oracleConnection = getOracleConnection();
        System.out.println(oracleConnection);
    }

    public static Connection getMysqlConnection() {
        //return mysqlConnection;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //url:   jdbc:mysql://ip:port/dbname
        //
        Connection connection = null;
        try {
            // connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db2202?user=root&password=root");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db2202", "root", "root");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println(connection);

        return connection;
    }

    public static Connection getOracleConnection() {
        // return oracleConnection;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //
            return DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "hr", "hr");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
